package com.ess.project.examine.grade.service;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ess.common.utils.StringUtils;
import com.ess.common.utils.text.Convert;
import com.ess.project.examine.grade.domain.ExamineGradeEntity;
import com.ess.project.examine.grade.mapper.ExamineGradeMapper;
import com.ess.project.examine.record.util.ExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

/**
 * @Description:
 * @Date: Created in 2:58 下午 2021/3/6
 * @Author: daihui
 * @Modified By:
 */
@Service("gradeService")
public class GradeServiceImpl extends ServiceImpl<ExamineGradeMapper, ExamineGradeEntity> implements IGradeService {

    @Resource
    private ExamineGradeMapper gradeMapper;


    @Override
    public List<ExamineGradeEntity> selectGradeList(ExamineGradeEntity grade) {
        QueryWrapper<ExamineGradeEntity> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(grade.getPersonnelCategory())) {
            wrapper.eq("personnel_category", grade.getPersonnelCategory());
        }
        if (StrUtil.isNotEmpty(grade.getExamPersonnel())) {
            wrapper.like("exam_personnel", grade.getExamPersonnel());
        }
        if (StrUtil.isNotEmpty(grade.getExamTime())) {
            wrapper.eq("exam_time", grade.getExamTime());
        }
        return gradeMapper.selectList(wrapper);
    }


    /**
     * 成绩评定
     * 计算总分，判断是否及格(5项目均及格)，并给出成绩评定
     */
    @Override
    public void export(String personnelCategory, String examTime, HttpServletResponse response) {
        QueryWrapper<ExamineGradeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("personnel_category", personnelCategory);
        queryWrapper.eq("exam_time", examTime);
        List<ExamineGradeEntity> exportData = gradeMapper.selectList(queryWrapper);

        if (null != exportData) {
            // 序号
            Stream.iterate(0, i -> i + 1).limit(exportData.size()).forEach(index -> exportData.get(index).setSerialNumber(index + 1));
            Map<String, Object> header = MapBuilder.create(new HashMap<String, Object>(2))
                    .put("personnelCategory", personnelCategory).put("examTime", examTime).build();
            try {
                String exportName = examTime + "_" + personnelCategory + "_体能训练考核成绩";
                String template = "template/exportTemplate.xlsx";
                ExcelUtil.writeExcelByTemplate(response, template, exportData, exportName, header, ExamineGradeEntity.class);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("数据异常");
            }
        }
    }

    @Override
    public List<ExamineGradeEntity> selectExamineTimeList() {
        return gradeMapper.selectExamineTimeList();
    }

    @Override
    public int deleteGradeByIds(String ids) {
        Long[] gradeIds = Convert.toLongArray(ids);
        return gradeMapper.deleteGradeByIds(gradeIds);
    }

    @Override
    public ExamineGradeEntity selectRecordById(Integer gradeId) {
        return gradeMapper.selectById(gradeId);
    }

    @Override
    public int updateGrade(ExamineGradeEntity grade) {
        return gradeMapper.updateById(grade);
    }

    @Override
    public Map<String,Object> getGradesByPersonnel(String examPersonnel,String examTimePar) {
        //结果集
        Map<String,Object> ret = new ConcurrentHashMap<>();
        //体型
        List<String> bodilyForms = new ArrayList<>();
        //3000米跑分数
        List<Integer> threeKmScore = new ArrayList<>();
        //单杠/俯卧撑分数
        List<Integer> horizontalbarPushupScore = new ArrayList<>();
        //仰卧起坐分数
        List<Integer> situpScore = new ArrayList<>();
        //30米×2蛇形跑分数
        List<Integer> snakeRunScore = new ArrayList<>();
        //考核时间
        List<String> examTime = new ArrayList<>();
        List<ExamineGradeEntity> lists = gradeMapper.getGradesByPersonnel(examPersonnel,examTimePar);
        for(ExamineGradeEntity entity : lists){
            if(examTime.contains(entity.getExamTime())){
                continue;
            }
            bodilyForms.add(entity.getBodilyForm());
            threeKmScore.add(entity.getThreeKmScore());
            horizontalbarPushupScore.add(entity.getHorizontalbarPushupScore());
            situpScore.add(entity.getSitupScore());
            snakeRunScore.add(entity.getSnakeRunScore());
            examTime.add(entity.getExamTime());
        }
        ret.put("bodilyForms", bodilyForms);
        ret.put("threeKmScore",threeKmScore );
        ret.put("horizontalbarPushupScore",horizontalbarPushupScore );
        ret.put("situpScore", situpScore);
        ret.put("snakeRunScore", snakeRunScore);
        ret.put("examTime", examTime);
        return ret;
    }

    @Override
    public Map<String,Object> getChartsDataForGlrycjdb(String examTimePar) {
        //结果集
        Map<String,Object> ret = new ConcurrentHashMap<>();
        //体型
        List<String> bodilyForms = new ArrayList<>();
        //3000米跑分数
        List<Integer> threeKmScore = new ArrayList<>();
        //单杠/俯卧撑分数
        List<Integer> horizontalbarPushupScore = new ArrayList<>();
        //仰卧起坐分数
        List<Integer> situpScore = new ArrayList<>();
        //30米×2蛇形跑分数
        List<Integer> snakeRunScore = new ArrayList<>();
        //人员分类
        List<String> ryfl = new ArrayList<>();
        List<ExamineGradeEntity> lists = gradeMapper.getChartsDataForGlrycjdb(examTimePar);
        for(ExamineGradeEntity entity : lists){
            if(ryfl.contains(entity.getPersonnelCategory())){
                continue;
            }
            bodilyForms.add(entity.getBodilyForm());
            threeKmScore.add(entity.getThreeKmScore());
            horizontalbarPushupScore.add(entity.getHorizontalbarPushupScore());
            situpScore.add(entity.getSitupScore());
            snakeRunScore.add(entity.getSnakeRunScore());
            ryfl.add(entity.getPersonnelCategory());
        }
        ret.put("bodilyForms", bodilyForms);
        ret.put("threeKmScore",threeKmScore );
        ret.put("horizontalbarPushupScore",horizontalbarPushupScore );
        ret.put("situpScore", situpScore);
        ret.put("snakeRunScore", snakeRunScore);
        ret.put("ryfl", ryfl);
        return ret;
    }

    @Override
    public Map<String,Object> getChartsDataForZtcjdb() {
        //结果集
        Map<String,Object> ret = new ConcurrentHashMap<>();
        //体型
        List<String> bodilyForms = new ArrayList<>();
        //3000米跑分数
        List<Integer> threeKmScore = new ArrayList<>();
        //单杠/俯卧撑分数
        List<Integer> horizontalbarPushupScore = new ArrayList<>();
        //仰卧起坐分数
        List<Integer> situpScore = new ArrayList<>();
        //30米×2蛇形跑分数
        List<Integer> snakeRunScore = new ArrayList<>();
        //考核时间
        List<String> examTime = new ArrayList<>();
        List<ExamineGradeEntity> lists = gradeMapper.getChartsDataForZtcjdb();
        for(ExamineGradeEntity entity : lists){
            if(examTime.contains(entity.getPersonnelCategory())){
                continue;
            }
            bodilyForms.add(entity.getBodilyForm());
            threeKmScore.add(entity.getThreeKmScore());
            horizontalbarPushupScore.add(entity.getHorizontalbarPushupScore());
            situpScore.add(entity.getSitupScore());
            snakeRunScore.add(entity.getSnakeRunScore());
            examTime.add(entity.getExamTime());
        }
        ret.put("bodilyForms", bodilyForms);
        ret.put("threeKmScore",threeKmScore );
        ret.put("horizontalbarPushupScore",horizontalbarPushupScore );
        ret.put("situpScore", situpScore);
        ret.put("snakeRunScore", snakeRunScore);
        ret.put("examTime", examTime);
        return ret;
    }

    @Override
    public List<ExamineGradeEntity> getAllPerson(String personnelCategory){
        return gradeMapper.getAllPerson(personnelCategory);
    }

    /**
     * 获取某一类别下的人员列表
     * @return
     * @author qlq
     * 2021-04-13
     */
    @Override
    public Map<String,Object> getDkcjdb(String dkcjdbPersonnelCategory, String[] dkcjdbPersonName, String khkm){
        Map<String,Object> ret = new ConcurrentHashMap<>();
        List<String> examTime = new ArrayList<>();
        List<Map<String,Object>> series = new ArrayList<>();

        ret.put("dkcjdbPersonName",dkcjdbPersonName);
        List<ExamineGradeEntity> datas = gradeMapper.getDkcjdb(dkcjdbPersonnelCategory,dkcjdbPersonName,khkm);
        List<String> times = gradeMapper.getDkcjdbTime(dkcjdbPersonnelCategory,dkcjdbPersonName);
        for (String per : dkcjdbPersonName){
            Map<String,Object> temp = new ConcurrentHashMap<>();
            double[] td = new double[times.size()];
            for (int i = 0; i < times.size(); i++){
                for(ExamineGradeEntity ee : datas){
                    if(StringUtils.isNotEmpty(ee.getExamPersonnel()) && ee.getExamPersonnel().equals(per) && StringUtils.isNotEmpty(ee.getExamTime()) && ee.getExamTime().equals(times.get(i))){
                        td[i] = ee.getScore();
                    }
                }
            }
            temp.put("name",per);
            temp.put("type","line");
            temp.put("data",td);
            series.add(temp);
        }
        ret.put("series",series);
        ret.put("times",times);
        return ret;
    }
}
